#include "mystring.h"

namespace yzw
{
	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n+1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n;
		}
	}

	void string::push_back(char ch)
	{
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 4 : 2 * _capacity);
		}

		_str[_size++] = ch;
	}

	void string::append(const char* str)
	{
		size_t ret = strlen(str);
		if (_capacity <= _size + ret)
		{
			reserve(_size + ret > 2 * _capacity ?  2 * _capacity: _size + ret);
		}
		strcpy(_str + _size, str);
		_size += ret;
	}

	string& string::operator+=(char ch)
	{
		push_back(ch);
		return *this;
	}

	string& string::operator+=(const char* str)
	{
		append(str);
		return *this;
	}

}